package com.wuye.action;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuye.bpm.ProcessManager;
import com.wuye.common.Constants;
import com.wuye.entity.PageCond;
import com.wuye.entity.Users;
import com.wuye.entity.Weixiu;
import com.wuye.service.WeixiuService;

@Controller
@RequestMapping("/weixiu")
public class WeixiuAction {
	@Resource
	private WeixiuService weixiuService;

	private final static String PROCESS_NAME = "weixiu";

	// 增加
	@RequestMapping("/save")
	public String save(Weixiu weixiu, HttpServletRequest request) {
		weixiuService.save(weixiu);
		ProcessDefinition process = ProcessManager.getInstance().getProcessByProcessDefinitionName(PROCESS_NAME);
		if (null == process) {
			ProcessManager.getInstance().deployProcess(PROCESS_NAME + Constants.PROCESS_SUFFIX);
		}
		Users user = (Users) request.getSession().getAttribute(Constants.SESSIONID);
		Map map = new HashMap();
		map.put("userId", user.getName());
		String processInstId = ProcessManager.getInstance().startProcess(PROCESS_NAME, null, map);

		List<Task> taskList = ProcessManager.getInstance().listMyTask(user.getName(), null);
		ProcessManager.getInstance().completeTask(taskList.get(0).getId());
		
		request.setAttribute("msg", "维修申请已提交");
		return "../sqwx.jsp";
	}

	// 删除
	@RequestMapping("/del")
	public String del(String str, HttpServletRequest request) {
		weixiuService.del(str);
		request.setAttribute("msg", "已删除");
		return "../wx.jsp";
	}

	// 更新
	@RequestMapping("/update")
	public String update(String str, HttpServletRequest request) {
		weixiuService.update(str);
		String id = (String) request.getAttribute("taskId");
		ProcessManager.getInstance().completeTask(id);

		request.setAttribute("msg", "已处理");
		return "../wx.jsp";
	}

	// 列表
	@RequestMapping("/allList")
	public @ResponseBody
	Map getAllList(String page, String rows, HttpServletRequest request) {
		int intPage = Integer.parseInt((page == null || page == "0") ? "1" : page);
		int number = Integer.parseInt((rows == null || rows == "0") ? "10" : rows);
		int start = (intPage - 1) * number;
		Users user = (Users) request.getSession().getAttribute(Constants.SESSIONID);
		PageCond pc = new PageCond();
		pc.setBeginIndex(start);
		pc.setLength(number);
		List<Task> taskList = ProcessManager.getInstance().listMyTask(user.getName(), pc);
		if (null != taskList) {
			for (Task task : taskList) {
				if (null != task.getTaskLocalVariables()) {
					Iterator<String> keyIterator = task.getTaskLocalVariables().keySet().iterator();
					while (keyIterator.hasNext()) {
						System.out.println(keyIterator.next());// key
						System.out.println(task.getTaskLocalVariables().get(keyIterator.next()));// value
					}
				}
			}
		}

		Map map = new HashMap();
		map.put("total", (null == taskList) ? 0 : taskList.size());

		JSONArray array = new JSONArray();
		String[] title = { "id", "neirong", "shijian", "username" };
		Iterator it = taskList.iterator();
		while (it.hasNext()) {
			Task aTask = (Task) it.next();
			JSONObject json = new JSONObject();
			json.put(title[0], aTask.getId());
			json.put(title[1], aTask.getName());
			json.put(title[2], aTask.getCreateTime().toString());
			json.put(title[3], aTask.getAssignee());
			array.add(json);
		}
		map.put("rows", array);
		return map;
	}

	// 查询
	@RequestMapping("/searchList")
	public @ResponseBody
	Map getSearchList(Weixiu weixiu, String page, String rows, String shijian1, String shijian2, HttpServletRequest request) throws Exception {
		int intPage = Integer.parseInt((page == null || page == "0") ? "1" : page);
		int number = Integer.parseInt((rows == null || rows == "0") ? "10" : rows);
		int start = (intPage - 1) * number;
		return weixiuService.getSearchList(weixiu, start, number, shijian1, shijian2);
	}

	public WeixiuService getWeixiuService() {
		return weixiuService;
	}

	public void setWeixiuService(WeixiuService weixiuService) {
		this.weixiuService = weixiuService;
	}
}
